package com.validators;

import java.lang.reflect.Field;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.apache.log4j.Logger;

import com.view.BaseView;

public class NotNullMultipleValidator implements
		ConstraintValidator<NotNullMultiple, BaseView> {

	private String propieades;
	private Logger log = Logger.getLogger(NotNullMultipleValidator.class);

	public void initialize(NotNullMultiple nnm) {
		propieades = nnm.property();
	}

	public boolean isValid(BaseView objeto, ConstraintValidatorContext cvContext) {

		boolean valid = false;

		String[] temp = propieades.split(",");

		for (int i = 0; i < temp.length; i++) {
			String objetoSplit = temp[i];

			Field field;
			Object value = null;
			try {
				field = objeto.getClass().getDeclaredField(objetoSplit);
				field.setAccessible(true);
				value = field.get(objeto);

				if (value != null && !"".equals(value)) {
					valid = true;
					break;
				}
			} catch (Throwable e) {
				log.error(
						"Error al validar el objeto en el NotNullMultipleValidator",
						e);
			}
		}

		return valid;
	}

}